{extend name="base:base" /}

{block name="body"}
<div class="layui-btn-group">
    <a class="layui-btn layui-btn-small layui-btn-normal editgroup" href="{:Url::build('AuthManager/editGroup')}">增加</a>
    <button class="layui-btn layui-btn-small layui-btn-warm ajax-post" url="{:Url::build('batchUpdate',['value'=>1])}" target-form="ids">启用</button>
    <button class="layui-btn layui-btn-small layui-btn-warm ajax-post " url="{:Url::build('batchUpdate',['value'=>0])}" target-form="ids" >禁用</button>
    <button class="layui-btn layui-btn-small layui-btn-danger ajax-post " url="{:Url::build('batchUpdate',['value'=>-1])}" target-form="ids">删除</button>
</div>

<table class="layui-hide" id="auth" lay-filter="auth"></table>
{/block}
{block name="script"}
<script type="text/html" id="statusTpl">
    {{#  if(d.status == 1){return "正常"; } else {return "禁用";} }}
</script>
<script type="text/html" id="competence">
    <a class="layui-btn layui-btn-mini" lay-event="access">访问授权</a>
    <a class="layui-btn layui-btn-mini" lay-event="user">成员授权</a>
</script>
<script type="text/html" id="operating">
    <a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-primary layui-btn-mini" lay-event="status">更改状态</a>
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="delete">删除</a>
</script>
<script type="text/javascript">
    UrlHighlight("{:Url::build('AuthManager/index')}");
    layui.use(['table', 'layer', 'jquery', 'form'],function () {
        table_reload = layui.table.render({
            elem: '#auth',
            url: "{:Url::build('AuthManager/authJson')}",
            page: true,
            method: 'post',
            limit: 10,
            id: "ids",
            loading: false,
            cols: [[ //table column
                {checkbox: true},
                {field: 'id', title: 'ID', width: 100},
                {field: 'title', title: '用户组', width: 200},
                {field: 'description', title: '描述', width: 200},
                {field: 'auth', title: '授权', width: 200,templet: '#competence'},
                {field: 'status', title: '状态', width: 200, templet: '#statusTpl'},
                {fixed: 'right', title: '操作', align: 'center', width: 200, toolbar: '#operating'},
            ]]
        });

        //监听工具条
        layui.table.on('tool(auth)', function (obj) {
            switch (obj.event) {
                case "status":
                    layui.layer.confirm('确定执行该操作吗?', function (index) {
                        layui.layer.close(index);
                        layui.$.post("{:Url::build('setStatus')}", {
                            value: Math.abs(obj.data.status - 1),
                            ids: obj.data.id
                        }, function (data) {
                            if (data.code !== 1) {
                                return alert_msg(data.msg, 0)
                            };
                            alert_msg(data.msg, 1);
                            setTimeout(function () {
                                table_reload.reload();
                            }, 1500);
                        });
                    });
                    break;
                case 'delete':
                    layui.layer.confirm('确定执行该操作吗?', function (index) {
                        obj.del();
                        layui.layer.close(index);
                        layui.$.post("{:Url::build('setStatus')}", {
                            value: -1,
                            ids: obj.data.id
                        }, function (data) {
                            if (data.code !== 1) {
                                return alert_msg(data.msg, 0)
                            };
                            alert_msg(data.msg, 1);
                            setTimeout(function () {
                                table_reload.reload();
                            }, 1500);
                        });
                    });
                    break;
                case 'edit':
                    layui.$.get("{:Url::build('AuthManager/editGroup')}", {id: obj.data.id}, function (str) {
                        if (str.code === 0) {
                            return alert_msg(str.msg);
                        };
                        pop_up =layui.layer.open({
                            type: 1,
                            title: "用户授权详情",
                            area: '550px',
                            content: str,
                        });
                    });
                    break;
                case 'access':
                    window.location.href = createURL("{:Url::build('AuthManager/access')}", {group_id: obj.data.id});
                    break;
                case 'user':
                    window.location.href = createURL("{:Url::build('AuthManager/user')}", {group_id: obj.data.id});
                    break;
                default:
                    layui.layer.msg('噢，天啊！你点了什么？');
                    break;
            }
        });


        //新增
        layui.$(".editgroup").on('click', function () {
            layui.$.get(layui.$(this).attr('href'), function (str) {
                if (str.code === 0) {
                    return alert_msg(str.msg);
                }
                pop_up=layui.layer.open({
                    type: 1,
                    title: "详情",
                    area: '550px',
                    content: str
                });
            });
            return false;
        });
    })
</script>
{/block}